package com.jdbc1234.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

/**
 * Shiro JDBC Realm 测试
 * @author : 章仕烜
 * @create : 2019/12/19
 **/
public class JdbcRealmTest {
    public static void main(String[] args) {
        // 读取配置文件，初始化SecurityManager工厂
        Factory<SecurityManager> securityManagerFactory =
                new IniSecurityManagerFactory("classpath:jdbc_realm.ini");
        // 获取securityManager实例
        SecurityManager securityManager = securityManagerFactory
                .getInstance();
        // 将securityManager绑定到securityUtils上
        SecurityUtils.setSecurityManager(securityManager);
        // 得到当前执行的用户
        Subject currentUser = SecurityUtils.getSubject();
        // 创建token令牌，用户名密码
        UsernamePasswordToken token = new UsernamePasswordToken("java1234","123456");

        try {
            // 身份认证
            currentUser.login(token);
            System.err.println("身份认证成功!");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.err.println("身份认证失败!");
        }
        currentUser.logout();
    }
}
